<template>
  <div class="login-container">
    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on" label-position="left">

      <!-- <div class="title-container">
        <h3 class="title-container">蓝湖房产管理系统
          <img src="@/assets/common/login-logo.png" alt="">
        </h3>
      </div> -->

      <div class="title-container">
        <h3 class="title">蓝湖房产管理系统</h3>
        <!-- <img src="@/assets/common/logBg.png" alt=""> -->
      </div>
      <!-- 注册用户名 -->
      <el-form-item prop="username">
        <span class="svg-container" />
        <el-input
          ref="username"
          v-model="loginForm.username"
          placeholder="输入您要注册的用户名"
          name="username"
          type="text"
          tabindex="1"
          auto-complete="on"
        />

      </el-form-item>

      <!-- 请输入注册电子邮箱   -->
      <el-form-item prop="email">
        <span class="svg-container" />
        <el-input
          ref="email"
          v-model="loginForm.email"
          placeholder="请输入您自定义的邮箱地址"
          name="email"
          type="text"
          tabindex="1"
          auto-complete="on"
        />
      </el-form-item>

      <!-- 密码 -->
      <el-form-item prop="password">
        <span class="svg-container">
          <svg-icon icon-class="password" />
        </span>
        <el-input
          :key="passwordType"
          ref="password"
          v-model="loginForm.password"
          :type="passwordType"
          placeholder="请确认您的密码"
          name="password"
          tabindex="2"
          auto-complete="on"
          @keyup.enter.native="handleLogin"
        />
        <span class="show-pwd" @click="showPwd">
          <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
        </span>
      </el-form-item>

      <el-button class="Btn" :loading="loading" type="primary" style="width:100%;margin-bottom:30px;" @click.native.prevent="handleLogin">点击注册</el-button>
      <!-- 注册页面 -->
      <div class="register">
        <router-link to="/login"> 使用已有账户登录</router-link>

      </div>
      <div class="tips">
        <span style="margin-right:20px;">username: admin</span>
        <span> password: any</span>
      </div>

    </el-form>
  </div>
</template>

<script>
// import { validUsername } from '@/utils/validate'
import { register } from '@/api/user'
import { Message } from 'element-ui'
export default {
  name: 'Login',
  data() {
    return {
      loginForm: {
        username: '', // 用户名 username
        email: '', // 电子邮箱 email
        password: '' // 密码 password
      },
      loginRules: {
        username: [
          { required: true, trigger: 'blur', message: '用户名不能为空' }

        ],
        email: [
          { required: true, message: '请输入邮箱地址', trigger: 'blur' }
        ],
        password: [
          { required: true, trigger: 'blur', message: '密码不能为空' },
          { min: 6, max: 12, message: '密码长度必须是6-12位' }
        ],
        passwordTwo: [
          { required: true, trigger: 'blur', message: '密码不能为空' },
          { min: 6, max: 12, message: '密码长度必须是6-12位' }
        ]

      },
      loading: false,
      passwordType: 'password',
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  methods: {
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    // handleLogin() {
    //   this.$refs.loginForm.validate().then(() => {
    //     console.log('验证通过')
    //   }).catch(() => {
    //     console.log('验证未通过')
    //   }) // console.log(valid) 当valid为true 表示正则验证通过
    // }
    async handleLogin() {
      await this.$refs.loginForm.validate()
      console.log('验证通过')
      // console.log(12345)
      const res = await register(this.loginForm)
      console.log(195, res)
      // 提示用户
      Message.success('恭喜您注册成功!')
      // const res = await login(this.loginForm)
      // console.log(118, res)
      // login(this.loginForm).then((res) => {
      //   console.log(120, res)
      // })
      // this.$store.dispatch('user/login', this.loginForm)
    }
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg:#283443;
$light_gray:rgb(123, 115, 124);
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}
 .el-form-item__error {
    color: #fff
  }
/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(255, 255, 255, 0.7); // 输入登录表单的背景色
    border-radius: 5px;
    color: #454545;
  }
}
</style>

<style lang="scss" scoped>
$bg:#2d3a4b;
$dark_gray:#889aa4;
$light_gray:#eee;

.login-container {
  .tips{
    text-align: center;
  }
.register{
  margin-bottom: 8px;
  color:yellow;
  text-align: center;
}
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;
      //登录背景图
  background-image: url('~@/assets/common/login.jpg'); // 设置背景图片
  background-position: center; // 将图片位置设置为充满整个屏幕

  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 160px 35px 0;
    margin: 0 auto;
    overflow: hidden;

  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
</style>
